﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
using System.Reflection;
namespace Wys.DataForm.CSharp
{
    
    public partial class UpdateMenu : Wys.Control.Vb.AutoAdjustFrom
    {

        int iMenutype = -1;
        ADODB.Command cm = new ADODB.Command();
        string strId = ""; //2010-04-24

        #region 构造函数
        public UpdateMenu()
        {
            InitializeComponent();
        }
        #endregion

        #region 加载子菜单
        public void LoadNodeData(TreeNode tn, String strMenuName)
        {
            ADODB.Recordset rs = null;
            rs = ct.GetTable("SELECT * FROM  XSCMenuBackUp where ParentMenu='" + strMenuName + "'");
            if (rs != null)
            {
                while (!rs.EOF)
                {
                    TreeNode tnChild = new TreeNode();
                    tnChild.Text = rs.Fields["TITLE"].Value.ToString();
                    if (String.IsNullOrEmpty(rs.Fields["CLASSNAME"].Value.ToString()))
                    {
                        tnChild.Name = rs.Fields["MENUNAME"].Value.ToString();
                        tnChild.Tag = "parentmenu";
                        tn.Nodes.Add(tnChild);
                        LoadNodeData(tnChild, tnChild.Name);
                    }
                    else
                    {
                        //tnChild.Name = rs.Fields["CLASSNAME"].Value.ToString(); //D-2010-04-24
                        tnChild.Name = rs.Fields["ID"].Value.ToString();  //A-2010-04-24
                        tnChild.Tag = "child";
                        tn.Nodes.Add(tnChild);
                    }
                    if (tnChild.Name == _lastSelectNode)
                    {
                        _selectNode = tnChild;
                    }
                    rs.MoveNext();
                }
            }
            rs.Close();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(rs); //20100531
        }
        #endregion

        #region 加载菜单
        public void LoadData()
        {
            _selectNode = null;
            this.trvRole.Nodes.Clear();
            ADODB.Recordset rs = ct.GetTable("SELECT * FROM  XSCMenuBackUp where ParentMenu is null");
            if (rs != null)
            {
                while (!rs.EOF)
                {
                    TreeNode tn = new TreeNode();
                    tn.Text = rs.Fields["TITLE"].Value.ToString();
                    tn.Name = rs.Fields["MENUNAME"].Value.ToString();
                    if (tn.Name == _lastSelectNode)
                    {
                        _selectNode = tn;
                    }
                    tn.Tag = "topmenu";
                    this.trvRole.Nodes.Add(tn);
                    rs.MoveNext();
                }
                rs.Close();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(rs); //20100531
                foreach (TreeNode tn in this.trvRole.Nodes)
                {
                    LoadNodeData(tn, tn.Name);
                }
            }
        }
        #endregion        

        #region 加载菜单图片
        public void LoadMenuImage()
        {
            //cboMenuFile.Items.Clear();
            //String strFolder = System.Configuration.ConfigurationManager.AppSettings["MenuFileFolder"];
            //System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(strFolder);
            //if (di != null)
            //{
            //    Image iTempImage = null;
            //    foreach (System.IO.FileInfo fi in di.GetFiles("*.ico"))
            //    {
            //        iTempImage = Image.FromFile(fi.FullName);
            //        if (iTempImage.Height == 16 && iTempImage.Width == 16)
            //        {
            //            this.cboMenuFile.Items.Add(fi.Name);
            //        }
                     
            //    }
            //}
        }
        #endregion

        #region 初始化事件
        private void UpdateMenu_Load(object sender, EventArgs e)
        {
            LoadData();
            LoadMenuImage();
        }
        #endregion

        #region 树形控件单击
        private TreeNode _selectNode = null;
        private String _lastSelectNode = "";
        private void trvRole_AfterSelect(object sender, TreeViewEventArgs e)
        {
            _lastSelectNode = e.Node.Name;
            if (e.Node.Tag.ToString() == "topmenu")
            {
                this.tabControl1.SelectedIndex = 0;
                LoadData(e.Node.Name, 0);
                this.groupBox2.Enabled = false;
                this.groupBox5.Enabled = true;
                this.groupBox6.Enabled = true;
                iMenutype = 0;
            }
            else if (e.Node.Tag.ToString() == "parentmenu")
            {
                this.tabControl1.SelectedIndex = 0;
                LoadData(e.Node.Name, 1);
                this.groupBox2.Enabled = false;
                this.groupBox5.Enabled = true;
                this.groupBox6.Enabled = true;
                iMenutype = 1;
            }
            else if (e.Node.Tag.ToString() == "child")
            {
                this.tabControl1.SelectedIndex = 1;
                LoadData(e.Node.Name, 2);
                this.groupBox2.Enabled = true;
                this.groupBox5.Enabled = false;
                this.groupBox6.Enabled = false;
                iMenutype = 2;
            }
        }
        #endregion

        #region 加载菜单数据
        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="strName">名称</param>
        /// <param name="itype">0 是顶层菜单 1 为父菜单 2 为子菜单</param>
        public void LoadData(String strName, int itype)
        {
            //清空控件
            this.txtParentTitle.Text = "";
            this.txtParentName.Text = "";

            this.txtName.Text = "";
            this.txtTitle.Text = "";
            this.cboParentShortKeys.Text = "";

            this.txtChildDllName.Text = "";
            this.txtChildName.Text = "";
            this.txtChildTitle.Text = "";
            this.cboKeys.Text = "";
            this.chkAlt.Checked = false;
            this.chkShift.Checked = false;
            this.chkCtrl.Checked = false;
            this.cboShortCuts.Text = "";

            //清空控件
            ADODB.Recordset rs = null;
            String strSql = String.Empty;
            String strName1 = string.Empty;
            String strTitle = string.Empty;
            if (itype == 0)
            {
                strSql = "select * from XSCMenuBackUp where  MENUNAME='" + strName + "'";
            }
            else if (itype == 1)
            {
                strSql = "select * from XSCMenuBackUp where  MENUNAME='" + strName + "'";
            }
            else if (itype == 2)
            {
                strSql = "select * from XSCMenuBackUp where  ID='" + strName + "'";
            }

            //rs = ReflectFunction(ct, "GetTable", new Object[] { strSql }) as ADODB.Recordset ;
            rs = ct.GetTable(strSql);
            if (rs != null)
            {
                if (rs.EOF == false)
                {
                    strId = rs.Fields["id"].Value.ToString();
                    if (itype == 0)
                    {
                        radTopMenu.Checked = true;
                        radNotTopMenu.Checked = false;
                        txtName.Text = rs.Fields["MENUNAME"].Value.ToString();
                        txtTitle.Text = rs.Fields["TITLE"].Value.ToString();
                        if (!String.IsNullOrEmpty(rs.Fields["ImagePath"].Value.ToString()))
                        {
                            this.cboMenuFile.Text = rs.Fields["ImagePath"].Value.ToString();
                        }
                        else
                        {
                            this.cboMenuFile.Text = "";
                            this.p1.Image = null;
                        }
                        if (!String.IsNullOrEmpty(rs.Fields["ShortCuts"].Value.ToString()))
                        {
                            this.cboParentShortKeys.Text = rs.Fields["ShortCuts"].Value.ToString();
                        }

                        if (Convert.ToInt32(rs.Fields["RequirePassword"].Value) == 1)
                        {
                            chk安全访问1.Checked = true;
                        }
                        else
                        {
                            chk安全访问1.Checked = false;
                        }
                    }
                    else if (itype == 1)
                    {

                        txtName.Text = rs.Fields["MENUNAME"].Value.ToString();
                        txtTitle.Text = rs.Fields["TITLE"].Value.ToString();
                        if (!String.IsNullOrEmpty(rs.Fields["ImagePath"].Value.ToString()))
                        {
                            this.cboMenuFile.Text = rs.Fields["ImagePath"].Value.ToString();
                        }
                        else
                        {
                            this.cboMenuFile.Text = "";
                            this.p1.Image = null;
                        }
                        if (!String.IsNullOrEmpty(rs.Fields["ShortCuts"].Value.ToString()))
                        {
                            this.cboParentShortKeys.Text = rs.Fields["ShortCuts"].Value.ToString();
                        }
                        if (Convert.ToInt32(rs.Fields["RequirePassword"].Value) == 1)
                        {
                            chk安全访问1.Checked = true;
                        }
                        else
                        {
                            chk安全访问1.Checked = false;
                        }

                        LoadParentData(rs.Fields["ParentMenu"].Value.ToString(), rs.Fields["MENUNAME"].Value.ToString(), ref strName1, ref strTitle, 0);
                        this.txtParentName.Text = strTitle;
                        this.txtParentTitle.Text = strName1;
                        //加载父菜单                        
                        radNotTopMenu.Checked = true;
                        radTopMenu.Checked = false;
                    }
                    else if (itype == 2)
                    {

                        this.txtChildTitle.Text = rs.Fields["TITLE"].Value.ToString();
                        this.txtChildName.Text = rs.Fields["CLASSNAME"].Value.ToString();
                        this.txtChildDllName.Text = rs.Fields["DLLNAME"].Value.ToString();
                        //判断是否为外部组件 2010-05-15
                        switch (Convert.ToInt16(rs.Fields["IsImportDll"].Value))
                        {
                            case 0:
                                chk外部组件.Checked = false;
                                rad外部Dll.Enabled = false;
                                rad外部EXE.Enabled = false;
                                break;
                            case 1:
                                chk外部组件.Checked = true;
                                rad外部EXE.Checked = true;
                                rad外部Dll.Enabled = true;
                                rad外部EXE.Enabled = true;
                                break;
                            case 2:
                                chk外部组件.Checked = true;
                                rad外部Dll.Checked = true;
                                rad外部Dll.Enabled = true;
                                rad外部EXE.Enabled = true;
                                break;
                            default:
                                chk外部组件.Checked = false;
                                rad外部Dll.Enabled = false;
                                rad外部EXE.Enabled = false;
                                break;
                        }
                        if (Convert.ToInt16(rs.Fields["IsAttach"].Value) == 0)
                        {
                            this.rad附加显示.Checked = false;
                            this.rad独立显示.Checked = true;
                        }
                        else
                        {
                            this.rad附加显示.Checked = true;
                            this.rad独立显示.Checked = false;
                        }
                        //判断是否为外部组件 2010-05-15
                        //2010-04-24
                        if(Convert.ToInt16(rs.Fields["IsImportDll"].Value)==0)
                        {
                        if (rs.Fields["IsParam"].Value.ToString() == "0")
                        {
                            SetFormParam(this.txtChildDllName.Text, this.txtChildName.Text);
                        }
                        else
                        {
                            SetFormParam(this.txtChildDllName.Text, this.txtChildName.Text, rs.Fields["ParamValue"].Value.ToString());
                        }
                        }
                        //2010-04-24
                        if (!String.IsNullOrEmpty(rs.Fields["ImagePath"].Value.ToString()))
                        {
                            this.cboMenuFile.Text = rs.Fields["ImagePath"].Value.ToString();
                        }
                        else
                        {
                            this.cboMenuFile.Text = "";
                            this.p1.Image = null;
                        }
                        if (!String.IsNullOrEmpty(rs.Fields["ShortCuts"].Value.ToString()))
                        {
                            this.cboShortCuts.Text = rs.Fields["ShortCuts"].Value.ToString();
                        }

                        if (!String.IsNullOrEmpty(rs.Fields["Keys"].Value.ToString()))
                        {
                            this.cboKeys.Text = rs.Fields["Keys"].Value.ToString();
                        }

                        if (!String.IsNullOrEmpty(rs.Fields["CtrlKey"].Value.ToString()))
                        {
                            if (Convert.ToBoolean(rs.Fields["CtrlKey"].Value))
                            {
                                this.chkCtrl.Checked = true;
                            }
                            else
                            {
                                this.chkCtrl.Checked = false;
                            }

                        }
                        else
                        {
                            this.chkCtrl.Checked = false;
                        }

                        if (!String.IsNullOrEmpty(rs.Fields["AltKey"].Value.ToString()))
                        {
                            if (Convert.ToBoolean(rs.Fields["AltKey"].Value))
                            {
                                this.chkAlt.Checked = true;
                            }
                            else
                            {
                                this.chkAlt.Checked = false;
                            }

                        }
                        else
                        {
                            this.chkAlt.Checked = false;
                        }

                        if (Convert.ToInt32(rs.Fields["RequirePassword"].Value) == 1)
                        {
                            chk安全访问.Checked = true;
                        }
                        else
                        {
                            chk安全访问.Checked = false;
                        }
                        if (!String.IsNullOrEmpty(rs.Fields["ShiftKey"].Value.ToString()))
                        {
                            if (Convert.ToBoolean(rs.Fields["ShiftKey"].Value))
                            {
                                this.chkShift.Checked = true;
                            }
                            else
                            {
                                this.chkShift.Checked = false;
                            }

                        }
                        else
                        {
                            this.chkShift.Checked = false;
                        }

                        LoadParentData(rs.Fields["ParentMenu"].Value.ToString(), rs.Fields["CLASSNAME"].Value.ToString(), ref strName1, ref strTitle, 1);
                        this.txtParentName.Text = strTitle;
                        this.txtParentTitle.Text = strName1;

                        radTopMenu.Checked = false;

                    }
                    //更新图片
                    if (String.IsNullOrEmpty(rs.Fields["ImagePath"].Value.ToString()))
                    {
                        p1.Image = null;
                    }
                    else
                    {
                        p1.Image = cDllRelease.GetImageFormResourceName(rs.Fields["ImagePath"].Value.ToString());
                    }
                }
                rs.Close();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(rs); //20100531
            }
        }
        #endregion        

        #region 加载子菜单的数据
        /// <summary>
        /// 加载子菜单的数据
        /// </summary>
        /// <param name="strParnetName">父菜单名称</param>
        /// <param name="strChildName">子菜单名称</param>
        /// <param name="strName"></param>
        /// <param name="strTitle"></param>
        public void LoadParentData(String strParnetName, String strChildName, ref String strName, ref String strTitle, int itype)
        {
            ADODB.Recordset rs = null;
            String strSql = string.Empty;
            if (itype == 0)
            {
                strSql = "select * from XSCMenuBackUp where MENUNAME='" + strParnetName + "'";
            }
            else
            {
                strSql = "select * from XSCMenuBackUp where MENUNAME='" + strParnetName + "'";
            }
            //rs = ReflectFunction(ct, "GetTable", new String[] { strSql }) as ADODB.Recordset;
            rs = ct.GetTable(strSql);
            if (rs != null)
            {
                if (rs.EOF == false)
                {
                    strName = rs.Fields["MENUNAME"].Value.ToString();
                    strTitle = rs.Fields["TITLE"].Value.ToString();
                }
                rs.Close();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(rs); //20100531
            }
        }
        #endregion

        #region 添加反射窗体
        private void btnSelect_Click(object sender, EventArgs e)
        {
            CSharpMenu cs = new CSharpMenu();
            if (cs.ShowDialog() == DialogResult.OK)
            {
                TreeNode tn = cs.GetSelectNode();
                if (tn != null)
                {
                    this.txtParentName.Text = tn.Text;
                    this.txtParentTitle.Text = tn.Tag.ToString();
                }
            }
        }
        #endregion       

        #region 关闭按钮
        private void myButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region 编辑按钮
         private void btnEdit_Click(object sender, EventArgs e)
        {
            if (trvRole.SelectedNode == null)
            {
                ShowMessageBox("请选择一个节点");
                return;
            }
            ADODB.Recordset rs = null;
            String strSql = String.Empty;

            #region 修改顶层菜单
            if (iMenutype == 0)
            {
                //还是顶层菜单
                if (this.radTopMenu.Checked)
                {
                  
                    if (this.txtTitle.Text.Trim() == String.Empty)
                    {
                        ShowMessageBox("标题不能为空为空");
                        this.txtTitle.Focus();
                        return;
                    }                   
                    //查找访问键
                    strSql = "select * from XSCMenuBackUp where ShortCuts='" + this.cboParentShortKeys.Text + "' and ParentMenu is null";
            
                    rs = ct.GetTable(strSql);
                    if (rs != null)
                    {
                        if (rs.EOF == false)
                        {
                            //是原来的
                            if (rs.Fields["MENUNAME"].Value.ToString() != this.trvRole.SelectedNode.Name)
                            {                              
                                ShowMessageBox("访问键已经存在:" + rs.Fields["TITLE"].Value.ToString());
                                rs.Close();
                                System.Runtime.InteropServices.Marshal.ReleaseComObject(rs); //20100531
                                return;
                            }
                            rs.Close();
                            System.Runtime.InteropServices.Marshal.ReleaseComObject(rs); //20100531
                        }
                        
                       
                    }

                }
                //变为父菜单
                if (this.radNotTopMenu.Checked)
                {
                    if (this.txtParentTitle.Text.Trim() == String.Empty)
                    {
                        ShowMessageBox("父菜单不能为空");
                        this.btnSelect.Focus();
                        return;
                    }
                    if (this.txtName.Text.Trim() == String.Empty)
                    {
                        ShowMessageBox("名称不能为空");
                        this.txtName.Focus();
                        return;
                    }
                    if (this.txtTitle.Text.Trim() == String.Empty)
                    {
                        ShowMessageBox("标题不能为空为空");
                        this.txtTitle.Focus();
                        return;
                    }
                    if (this.txtParentTitle.Text.Trim() == this.txtName.Text.Trim())
                    {
                        ShowMessageBox("两者是相同的");
                        this.btnSelect.Focus();
                        return;
                    }
                   

                    //查找访问键
                    strSql = "select * from XSCMenuBackUp where ShortCuts='" + this.cboParentShortKeys.Text + "' and ParentMenu='"+this.txtName.Text+"'";
                    //rs = ReflectFunction(ct, "GetTable", new Object[] { strSql }) as ADODB.Recordset;
                    rs = ct.GetTable(strSql);
                    if (rs != null)
                    {
                        if (rs.EOF == false)
                        {
                            //是原来的
                            if (rs.Fields["MENUNAME"].Value.ToString() != this.trvRole.SelectedNode.Name)
                            {                               
                                ShowMessageBox("访问键已经存在:" + rs.Fields["TITLE"].Value.ToString());
                                rs.Close();
                                System.Runtime.InteropServices.Marshal.ReleaseComObject(rs); //20100531
                                return;
                            }
                            rs.Close();
                            System.Runtime.InteropServices.Marshal.ReleaseComObject(rs); //20100531
                        }
                        
                       
                    }
                }               
               
                strSql = "select * from XSCMenuBackUp where MENUNAME='" + this.trvRole.SelectedNode.Name + "'";
                //rs = ReflectFunction(ct, "GetUpdateRs", new Object[] { strSql }) as ADODB.Recordset;
                rs = ct.GetUpdateRs(strSql);
                if (rs != null)
                {
                    if (rs.EOF == false)
                    {
                        rs.Fields["TITLE"].Value = this.txtTitle.Text.Trim();
                        if (this.radNotTopMenu.Checked)
                        {
                            rs.Fields["ParentMenu"].Value = this.txtParentTitle.Text.Trim();
                        }
                        if (this.radTopMenu.Checked)
                        {
                            rs.Fields["ParentMenu"].Value = DBNull.Value;
                        }

                        if (!String.IsNullOrEmpty(this.cboParentShortKeys.Text))
                        {
                            rs.Fields["ShortCuts"].Value = this.cboParentShortKeys.Text;
                        }
                        //if (this.p1.Image != null)
                        //{
                        //    rs.Fields["ImageContent"].AppendChunk(this.Image2Bytes(this.p1.Image));
                        //}
                        //2009-07-18 -A
                        if (!String.IsNullOrEmpty(this.cboMenuFile.Text))
                        {
                            rs.Fields["ImagePath"].Value = this.cboMenuFile.Text.Trim();
                        }
                        //2009-07-18 -A
                        if (chk安全访问1.Checked)
                        {
                            rs.Fields["RequirePassword"].Value = 1;
                        }
                        else
                        {
                            rs.Fields["RequirePassword"].Value = 0;
                        }
                       

                    }
                    rs.Update(System.Reflection.Missing.Value, System.Reflection.Missing.Value);
                    rs.Close();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(rs); //20100531
                }

                

                ShowMessageBox("更新成功");
                //刷新菜单
                LoadData();
                if (_selectNode != null)
                {
                    this.trvRole.SelectedNode = _selectNode;
                }
            }
            #endregion
        
            #region 修改父菜单
            if (iMenutype == 1)
            {
                //还是顶层菜单
                if (this.radTopMenu.Checked)
                {
                    if (this.txtName.Text.Trim() == String.Empty)
                    {
                        ShowMessageBox("名称不能为空");
                        this.txtName.Focus();
                        return;
                    }
                    if (this.txtTitle.Text.Trim() == String.Empty)
                    {
                        ShowMessageBox("标题不能为空为空");
                        this.txtTitle.Focus();
                        return;
                    }
                    ////验证图片是否正确
                    //if (this.p1.Image != null)
                    //{
                    //    if (p1.Image.Width != 16 || p1.Image.Height != 16)
                    //    {
                    //        ShowMessageBox("请选择16×16的图片");
                    //        return;
                    //    }
                    //}
                    //查找名称
                                 
                    //查找访问键
                    //strSql = "select * from XSCMenuBackUp where ShortCuts='" + this.cboParentShortKeys.Text + "' and ParentMenu is null";
                    ////rs = ReflectFunction(ct, "GetTable", new Object[] { strSql }) as ADODB.Recordset;
                    //rs=ct.GetTable(strSql);
                    //if (rs != null)
                    //{
                    //    if (rs.EOF == false)
                    //    {
                    //        //是原来的
                    //        if (rs.Fields["MENUNAME"].Value.ToString() != this.trvRole.SelectedNode.Name)
                    //        {                               
                    //            ShowMessageBox("访问键已经存在:" + rs.Fields["TITLE"].Value.ToString());
                    //            rs.Close();
                    //            System.Runtime.InteropServices.Marshal.ReleaseComObject(rs); //20100531
                    //            return;
                    //        }
                    //        rs.Close();
                    //        System.Runtime.InteropServices.Marshal.ReleaseComObject(rs); //20100531
                    //    }
                    //}

                }
                //变为父菜单
                if (this.radNotTopMenu.Checked)
                {
                    if (this.txtParentTitle.Text.Trim() == String.Empty)
                    {
                        ShowMessageBox("父菜单不能为空");
                        this.btnSelect.Focus();
                        return;
                    }
                    if (this.txtName.Text.Trim() == String.Empty)
                    {
                        ShowMessageBox("名称不能为空");
                        this.txtName.Focus();
                        return;
                    }
                    if (this.txtTitle.Text.Trim() == String.Empty)
                    {
                        ShowMessageBox("标题不能为空为空");
                        this.txtTitle.Focus();
                        return;
                    }
                    ////验证图片是否正确
                    //if (this.p1.Image != null)
                    //{
                    //    if (p1.Image.Width != 16 || p1.Image.Height != 16)
                    //    {
                    //        ShowMessageBox("请选择16×16的图片");
                    //        return;
                    //    }
                    //}
                    //查找名称
             
                    //查找访问键
                    //strSql = "select * from XSCMenuBackUp where ShortCuts='" + this.cboParentShortKeys.Text + "' and ParentMenu='" + this.txtParentTitle.Text + "'";
                    //rs = ReflectFunction(ct, "GetTable", new Object[] { strSql }) as ADODB.Recordset;
                    //rs=ct.GetTable(strSql);
                    //if (rs != null)
                    //{
                    //    if (rs.EOF == false)
                    //    {
                    //        是原来的
                    //        if (rs.Fields["MENUNAME"].Value.ToString() != this.trvRole.SelectedNode.Name)
                    //        {                               
                    //            ShowMessageBox("访问键已经存在:" + rs.Fields["TITLE"].Value.ToString());
                    //            rs.Close();
                    //            System.Runtime.InteropServices.Marshal.ReleaseComObject(rs); //20100531
                    //            return;
                    //        }
                    //        rs.Close();
                    //        System.Runtime.InteropServices.Marshal.ReleaseComObject(rs); //20100531
                    //    }
                    //}
                }
                if (this.txtParentTitle.Text.Trim() == this.txtName.Text.Trim())
                {
                    ShowMessageBox("两者是相同的");
                    this.btnSelect.Focus();
                    return;
                }
                strSql = "select * from XSCMenuBackUp where MENUNAME='" + this.trvRole.SelectedNode.Name + "'";
                //rs = ReflectFunction(ct, "GetUpdateRs", new Object[] { strSql }) as ADODB.Recordset;
                rs=ct.GetUpdateRs(strSql);
                if (rs != null)
                {
                    if (rs.EOF == false)
                    {
                        rs.Fields["TITLE"].Value = this.txtTitle.Text.Trim();
                        if (this.radNotTopMenu.Checked)
                        {
                            rs.Fields["ParentMenu"].Value = this.txtParentTitle.Text.Trim();
                        }
                        if (this.radTopMenu.Checked)
                        {
                            rs.Fields["ParentMenu"].Value = DBNull.Value;
                        }

                        if (!String.IsNullOrEmpty(this.cboParentShortKeys.Text))
                        {
                            rs.Fields["ShortCuts"].Value = this.cboParentShortKeys.Text;
                        }
                        //2009-07-18 -A
                        if (!String.IsNullOrEmpty(this.cboMenuFile.Text))
                        {
                            rs.Fields["ImagePath"].Value = this.cboMenuFile.Text;
                        }
                        //2009-07-18 -A

                        if (chk安全访问1.Checked)
                        {
                            rs.Fields["RequirePassword"].Value = 1;
                        }
                        else
                        {
                            rs.Fields["RequirePassword"].Value = 0;
                        }
                    }
                    rs.Update(System.Reflection.Missing.Value, System.Reflection.Missing.Value);
                    rs.Close();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(rs); //20100531
                }
                ShowMessageBox("更新成功");
                //刷新菜单
                LoadData();
                //查找访问键
                if (_selectNode != null)
                {
                    this.trvRole.Focus();
                    this.trvRole.SelectedNode = _selectNode;
                }
            }
            #endregion

            #region 修改子菜单
            if (iMenutype == 2)
            {
                if (this.txtParentTitle.Text.Trim() == String.Empty)
                {
                    ShowMessageBox("父菜单不能为空");
                    this.btnSelect.Focus();
                    return;
                }

                if (this.txtChildName.Text.Trim() == String.Empty)
                {
                    ShowMessageBox("名称不能为空");
                    this.txtChildName.Focus();
                    return;
                }
                if (this.txtChildDllName.Text.Trim() == String.Empty)
                {
                    ShowMessageBox("类型名称不能为空");
                    this.txtChildDllName.Focus();
                    return;
                }

                if (this.txtChildTitle.Text.Trim() == String.Empty)
                {
                    ShowMessageBox("标题不能为空");
                    this.txtChildTitle.Focus();
                    return;
                }             
        
                ////查找访问键
                //strSql = "select * from XSCMenuBackUp where ShortCuts='" + this.cboShortCuts.Text + "' and ParentMenu='" + this.txtParentTitle.Text + "'";
                ////rs = ReflectFunction(ct, "GetTable", new Object[] { strSql }) as ADODB.Recordset;
                //rs=ct.GetTable(strSql);
                //if (rs != null)
                //{
                //    if (rs.EOF == false)
                //    {
                //        //是原来的
                //        if (rs.Fields["CLASSNAME"].Value.ToString() != this.trvRole.SelectedNode.Name)
                //        {                          
                //            ShowMessageBox("访问键已经存在:" + rs.Fields["TITLE"].Value.ToString());
                //            rs.Close();
                //            System.Runtime.InteropServices.Marshal.ReleaseComObject(rs); //20100531
                //            return;
                //        }
                //        rs.Close();
                //        System.Runtime.InteropServices.Marshal.ReleaseComObject(rs); //20100531
                //    }
                //}
                ////查找快捷键
                ////判断快捷键是否唯一
                //if (cboKeys.Enabled == true)
                //{
                //    if (cboKeys.Text == string.Empty)
                //    {
                //        ShowMessageBox("快捷键不能为空");
                //        cboKeys.Focus();
                //        return;
                //    }
                //    else
                //    {
                //        strSql = "select * from XSCMenuBackUp where 1=1 ";
                //        strSql += " AND  Keys='" + cboKeys.Text + "' ";
                //        if (chkCtrl.Checked)
                //        {
                //            strSql += " AND  CtrlKey=1 ";
                //        }
                //        else
                //        {
                //            strSql += " AND  CtrlKey=0 ";
                //        }

                //        if (chkShift.Checked)
                //        {
                //            strSql += " AND  ShiftKey=1 ";
                //        }
                //        else
                //        {
                //            strSql += " AND  ShiftKey=0 ";
                //        }
                //        if (chkAlt.Checked)
                //        {
                //            strSql += " AND  AltKey=1 ";
                //        }
                //        else
                //        {
                //            strSql += " AND  AltKey=0 ";
                //        }
                //    }
                //}
                //rs = ReflectFunction(ct, "GetTable", new Object[] { strSql }) as ADODB.Recordset;
                //rs=ct.GetTable(strSql);
                //if (rs != null)
                //{
                //    if (rs.EOF == false)
                //    {
                //        if (rs.Fields["CLASSNAME"].Value.ToString() != trvRole.SelectedNode.Name)
                //        {                          
                //            ShowMessageBox("快捷键键已经存在:" + rs.Fields["TITLE"].Value.ToString());
                //            this.cboKeys.Focus();
                //            rs.Close();
                //            System.Runtime.InteropServices.Marshal.ReleaseComObject(rs); //20100531
                //            return;
                //        }
                //        rs.Close();
                //        System.Runtime.InteropServices.Marshal.ReleaseComObject(rs); //20100531
                //    }
                    
                //}

                //更新数据
                //strSql = "select * from XSCMenuBackUp where CLASSNAME='" + this.trvRole.SelectedNode.Name + "'"; //D-2010-04-24
                strSql = "select * from XSCMenuBackUp where ID='" + this.strId + "'"; //A-2010-04-24
                //rs = ReflectFunction(ct, "GetUpdateRs", new Object[] { strSql }) as ADODB.Recordset ;
                rs=ct.GetUpdateRs(strSql);
                if (rs != null)
                {
                    if (rs.EOF == false)
                    {
                        rs.Fields["TITLE"].Value = this.txtChildTitle.Text;
                        rs.Fields["CLASSNAME"].Value = this.txtChildName.Text;
                        rs.Fields["DLLNAME"].Value = this.txtChildDllName.Text;
                        rs.Fields["ParentMenu"].Value = this.txtParentTitle.Text;
                        if (!String.IsNullOrEmpty(this.cboShortCuts.Text))
                        {
                            rs.Fields["ShortCuts"].Value = this.cboShortCuts.Text;
                        }
                        if (rad附加显示.Checked)
                        {
                            rs.Fields["IsAttach"].Value = 1;
                        }
                        else
                        {
                            rs.Fields["IsAttach"].Value = 0;
                        }

                        if (chk安全访问.Checked)
                        {
                            rs.Fields["RequirePassword"].Value = 1;
                        }
                        else
                        {
                            rs.Fields["RequirePassword"].Value = 0;
                        }
                        
                        //判断是否为外部组件 2010-05-15
                        if (chk外部组件.Checked)
                        {
                            if (rad外部EXE.Checked)
                            {
                                rs.Fields["IsImportDll"].Value = 1;
                            }
                            else if (rad外部Dll.Checked)
                            {
                                rs.Fields["IsImportDll"].Value = 2;
                            }
                            else
                            {
                                rs.Fields["IsImportDll"].Value = 3;
                            }
                        }
                        else
                        {
                            rs.Fields["IsImportDll"].Value = 0;
                        }
                        //判断是否为外部组件 2010-05-15
                        if (cboKeys.Enabled = true && cboKeys.Text != "")
                        {
                            rs.Fields["keys"].Value = this.cboKeys.Text;
                            if (chkCtrl.Checked)
                            {
                                rs.Fields["CtrlKey"].Value = 1;
                            }
                            else
                            {
                                rs.Fields["CtrlKey"].Value = 0;
                            }
                            if (chkAlt.Checked)
                            {
                                rs.Fields["AltKey"].Value = 1;
                            }
                            else
                            {
                                rs.Fields["AltKey"].Value = 0;
                            }
                            if (chkShift.Checked)
                            {
                                rs.Fields["ShiftKey"].Value = 1;
                            }
                            else
                            {
                                rs.Fields["ShiftKey"].Value = 0;
                            }
                        }
                        //2009-07-18 -A
                        if (!String.IsNullOrEmpty(this.cboMenuFile.Text))
                        {
                            rs.Fields["ImagePath"].Value = this.cboMenuFile.Text;
                        }
                        //2009-07-18 -A

                        //2010-04-24
                        //是否使用参数
                        bool bParam = false;
                        foreach (String str in alValue)
                        {
                            if (str != "")
                            {
                                bParam = true;
                                break;
                            }
                        }
                        if (bParam)
                        {
                            rs.Fields["IsParam"].Value = 1;
                            string strParam = "";
                            for (int i = 0; i < alParam.Count; i++)
                            {
                                strParam += alParam[i] + ":" + alValue[i]+";"; 
                            }
                            rs.Fields["ParamValue"].Value = strParam;
                        }
                        else
                        {
                            rs.Fields["IsParam"].Value = 0;
                        }
                        //2010-04-24
                    }
                    rs.Update(System.Reflection.Missing.Value, System.Reflection.Missing.Value);
                    rs.Close();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(rs); //20100531
                }

                ShowMessageBox("更新成功");
                //刷新菜单
                LoadData();
               
               
            }
            #endregion         
        }
        #endregion       

        #region 是否是顶层菜单
        private void radTopMenu_CheckedChanged(object sender, EventArgs e)
        {
             if (radTopMenu.Checked)
             {
                 groupBox1.Enabled = false;
             }
             else
             {
                 groupBox1.Enabled = true;
             }
        }
        #endregion        

        #region 快捷键的控制
        private void chkCtrl_CheckStateChanged(object sender, EventArgs e)
        {
            if ((chkAlt.Checked == false) && (chkCtrl.Checked == false) && (chkShift.Checked == false))
            {
                cboKeys.Enabled = false;
            }
            else
            {
                cboKeys.Enabled = true;
            }
        }
        #endregion       

        #region 菜单图标选择按钮
        private void btnSelectImage_Click(object sender, EventArgs e)
        {
            MenuIcoManager mmc = new MenuIcoManager();
            if (mmc.ShowDialog() == DialogResult.OK)
            {
                if (!String.IsNullOrEmpty(mmc.IconName))
                {
                    cboMenuFile.Text = mmc.IconName;
                    p1.Tag = mmc.IconName;
                    p1.Image = this.cDllRelease.GetImageFormResourceName(cboMenuFile.Text);
                }
            }
        }
        #endregion

        #region 字节数组和图片的转换
        private Byte[] Image2Bytes(Image image)
        {

            byte[] Ret = null;
            try
            {
                using (MemoryStream ms = new MemoryStream())
                {

                    image.Save(ms, ImageFormat.Jpeg);
                    Ret = ms.ToArray();
                }
            }
            catch (Exception me)
            {
                //SaveException(ee);//A-2009-10-11
                UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, me, MethodBase.GetCurrentMethod().GetType());
                return null;
            }

            return Ret;
        }

        public Image ByteArrayToImage(byte[] byteArrayIn, int count)
        {
            try
            {
                MemoryStream ms = new MemoryStream(byteArrayIn, 0, count);
                Image returnImage = Image.FromStream(ms);
                return returnImage;
            }
            catch (Exception me)
            {
                //SaveException(me);//D-2009-10-11
                UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, me, MethodBase.GetCurrentMethod().GetType());//A-2009-10-11 
                return null;
            }

        }

        #endregion
        
        #region 删除菜单
        private void btnDel_Click(object sender, EventArgs e)
        {
            if (trvRole.SelectedNode == null)
            {
                ShowMessageBox("请选择一个节点");
                return;
            }
            if (ShowMessageResult("确信删除菜单") == DialogResult.No)
            {
                return;
            }
            if (iMenutype == 0)
            {
                DeleteMenu(this.txtName.Text);
                
            }
            if (iMenutype == 1)
            {
                DeleteMenu(this.txtName.Text);
                
            }
            if (iMenutype == 2)
            {
                DeleteMenuItem(this.strId);
                
            }
            ShowMessageBox("删除成功");
            //刷新菜单
            LoadData();
           
        }
        //直接删除子菜单
        private void DeleteMenuItem(String strMenuItemName)
        {
            String strSql = "";
            strSql = "Delete from XSCMenuBackUp where  IsParentMenu=0 and id='" + strMenuItemName + "'";
            //追加删除时的参数判断

            //追加删除时的参数判断
            cm.CommandText = strSql; 
            if (ct.ExcCommand(cm))
            {
                //添加删除记录功能  A-2010-03-24
                ct.AddDeleteDetal("XSCMenuBackUp", new String[] { "IsParentMenu", "id", "" }, new String[] { "0", strMenuItemName, "", "" }, this.strName);
                //添加删除记录功能  A-2010-03-24
            }
            else
            {
                
            }
        }
        //删除带子菜单的菜单
        private void DeleteMenu(String strParentMenuName)
        {
            try
            {
                    //删除它的所有子菜单(没有子菜单)
                    String strSql  ="";
                    strSql = "Delete from XSCMenuBackUp where  IsParentMenu=0 and ParentMenu='" + strParentMenuName + "'";               
                    cm.CommandText = strSql;
                    if (ct.ExcCommand(cm))
                    {
                        //添加删除记录功能  A-2010-03-24
                        ct.AddDeleteDetal("XSCMenuBackUp", new String[] { "IsParentMenu", "ParentMenu", "" }, new String[] { "0", strParentMenuName, "", "" }, this.strName);
                        //添加删除记录功能  A-2010-03-24
                    }
                    //查询父菜单(有子菜单)
                    strSql = "select * from XSCMenuBackUp where  IsParentMenu=1 and ParentMenu='"+strParentMenuName+"'";
                    ADODB.Recordset rs = ct.GetTable(strSql);
                    if (rs != null)
                    {
                        if (rs.EOF == false)
                        {
                            while (rs.EOF == false)
                            {
                                DeleteMenu(rs.Fields["MENUNAME"].Value.ToString());
                                rs.MoveNext();
                            }
                        }
                        rs.Close();
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(rs); //20100531
                    }
                    rs=null;
                   //删除自己
                   strSql = "Delete from XSCMenuBackUp where  IsParentMenu=1 and MENUNAME='" + strParentMenuName + "'";
                   cm.CommandText = strSql;
                   if (ct.ExcCommand(cm))
                   {
                       //添加删除记录功能  A-2010-03-24
                       ct.AddDeleteDetal("XSCMenuBackUp", new String[] { "IsParentMenu", "MENUNAME", "" }, new String[] { "1", strParentMenuName, "", "" }, this.strName);
                       //添加删除记录功能  A-2010-03-24
                   }


            }
            catch (Exception me)
            {
                //SaveException(ee);//D-2009-10-11
                UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, me, MethodBase.GetCurrentMethod().GetType());//A-2009-10-11 
            }
           
        }
        #endregion   

        #region 选择父菜单
        private void myButton3_Click(object sender, EventArgs e)
        {
            ChildSelect cs = new ChildSelect();
            cs.txtPath.Text  = Path.Combine(this.GetCurrentDirectory(), txtChildDllName.Text);
            cs.txtTitle.Text = txtChildTitle.Text;
            if (cs.ShowDialog() == DialogResult.OK)
            {
                this.txtChildName.Text = cs.ClassName1;
                this.txtChildTitle.Text = cs.Title;
                this.txtChildDllName.Text = cs.DllName;
                if (!cs.是否外部)
                {
                    SetFormParam(cs.DllName, cs.ClassName1);
                }
            }
        }
        #endregion

        #region 设置窗体的参数 2010-04-24
        private void myButton1_Click(object sender, EventArgs e)
        {
            using (FormParam fp = new FormParam())
            {
                for (int i = 0; i < alParam.Count; i++)
                {
                    if (i == 0)
                    {
                        fp.lbl1.Text = alParam[0].ToString();
                        if (alValue.Count > 0)
                        {
                            fp.txt1.Text = alValue[0].ToString();
                        }
                        fp.txt1.Enabled = true;
                    }
                    else if (i == 1)
                    {
                        fp.lbl2.Text = alParam[1].ToString();
                        if (alValue.Count > 1)
                        {
                            fp.txt2.Text = alValue[1].ToString();
                        }
                        fp.txt2.Enabled = true;
                    }
                    else if (i == 2)
                    {
                        fp.lbl3.Text = alParam[2].ToString();
                        if (alValue.Count > 2)
                        {
                            fp.txt3.Text = alValue[2].ToString();
                        }
                        fp.txt3.Enabled = true;
                    }
                    else if (i == 3)
                    {
                        fp.lbl4.Text = alParam[3].ToString();
                        if (alValue.Count > 3)
                        {
                            fp.txt4.Text = alValue[3].ToString();
                        }
                        fp.txt4.Enabled = true;
                    }
                    else if (i == 4)
                    {
                        fp.lbl5.Text = alParam[4].ToString();
                        if (alValue.Count > 4)
                        {
                            fp.txt5.Text = alValue[4].ToString();
                        }
                        fp.txt5.Enabled = true;
                    }
                }
                if (fp.ShowDialog() == DialogResult.OK)
                {
                    if(fp.txt1.Enabled)
                    {
                       alValue[0] = fp.txt1.Text.TrimEnd();
                    }
                    if (fp.txt2.Enabled)
                    {
                        alValue[1] = fp.txt2.Text.TrimEnd();
                    }
                    if (fp.txt3.Enabled)
                    {
                        alValue[2] = fp.txt3.Text.TrimEnd();
                    }
                    if (fp.txt4.Enabled)
                    {
                        alValue[3] = fp.txt4.Text.TrimEnd();
                    }
                    if (fp.txt5.Enabled)
                    {
                        alValue[4] = fp.txt5.Text.TrimEnd();
                    }
                }
            }
        }
        #endregion      
        
        #region 设置参数 2010-04-24
        System.Collections.ArrayList alParam = new System.Collections.ArrayList();
        System.Collections.ArrayList alValue= new System.Collections.ArrayList();
        private void SetFormParam(String strDll,String ClassName)
        {

            btnParam.Enabled = false;
            alParam.Clear();
            alValue.Clear();
            String strPath = Path.Combine(this.GetCurrentDirectory(), strDll);
            System.Reflection.Assembly tempas = System.Reflection.Assembly.LoadFile(strPath);
            System.Type ty = tempas.GetType(ClassName);
            if (ty != null)
            {
                PropertyInfo[] pis = ty.GetProperties();
                foreach (PropertyInfo pi in pis)
                {
                    if (pi.Name.StartsWith("选项参数_"))
                    {
                        alParam.Add(pi.Name);
                        alValue.Add("");
                    }
                }
                if (alParam.Count > 0)
                {
                    btnParam.Enabled = true;
                }
            }
        }
        #endregion

        #region 设置参数 2010-04-24
        /// <summary>
        /// 查询参数
        /// </summary>
        /// <param name="strDll">组件的全名</param>
        /// <param name="ClassName">类的名称</param>
        /// <param name="strParamVlaue">数据库的参数值</param>
        private void SetFormParam(String strDll, String ClassName,String strParamVlaue)
        {
            btnParam.Enabled = false;
            alParam.Clear();
            alValue.Clear();
            String strPath = Path.Combine(this.GetCurrentDirectory(), strDll);
            System.Reflection.Assembly tempas = System.Reflection.Assembly.LoadFile(strPath);
            System.Type ty = tempas.GetType(ClassName);
            if (ty != null)
            {
                PropertyInfo[] pis = ty.GetProperties();
                foreach (PropertyInfo pi in pis)
                {
                    if (pi.Name.StartsWith("选项参数_"))
                    {
                        alParam.Add(pi.Name);
                        alValue.Add("");
                    }
                }
                //参数值格式
                //类型:1;标题:英语报表
                if (strParamVlaue != null)
                {
                    foreach (String str in strParamVlaue.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
                    {
                        if (str != "")
                        {
                            String[] value = str.Split(":".ToCharArray());
                            if (value.Length == 2)
                            {
                                int index = alParam.IndexOf(value[0]);
                                if (index >= 0)
                                {
                                    alValue[index] = value[1];
                                }
                            }
                        }
                    }
                }
                if (alParam.Count > 0)
                {
                    btnParam.Enabled = true;
                }
            }
            
        }
        #endregion

        #region 追加从外部调用 2010-05-15
        private void chk外部组件_CheckedChanged(object sender, EventArgs e)
        {
            if (chk外部组件.Checked)
            {
                rad外部EXE.Enabled = true;
                rad外部Dll.Enabled = true;              
            }
            else
            {
                rad外部EXE.Enabled = false;
                rad外部Dll.Enabled = false;              
            }
        }
        #endregion  
    }
}
